package com.vodone.v1pin.common.view
{
	import com.vodone.v1pin.core.event.V1webEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import mx.controls.Image;
	import mx.events.FlexEvent;
	import mx.preloaders.DownloadProgressBar;
	import mx.preloaders.IPreloaderDisplay;
	
	public class CustomPreloader extends DownloadProgressBar implements IPreloaderDisplay
	{
		private var dpbImageControl:Loader;
		
		[Embed(source='assets/loading/fbj.png',scaleGridLeft=9,
			scaleGridRight=10,
			scaleGridTop=10,
			scaleGridBottom=11)] 
		public var bg:Class;
		
		[Bindable]  
		[Embed(source='assets/background/bj.png')]  
		public var bg0:Class;
		
		[Embed(source='assets/background/v1.png')] 
		public var v1:Class; 
		
		[Embed(source='assets/background/zuo_bj.png')]  
		public var bg1:Class;
		
		[Embed(source='assets/background/you_bj.png')]  
		public var bg2:Class;
		
		[Bindable]  
		[Embed(source='assets/background/shu.png')]  
		public var bg3:Class;
		
		[Bindable]  
		[Embed(source='assets/loading/loading3.swf')]  
		public var loading:Class;
		
		public function CustomPreloader()
		{
			super();
//			var bg:LoadingViewContent = new  LoadingViewContent();
//			this.addChild( bg );
		}
		
		protected function draw():void
		{
		}
		
		override public function set preloader(preloader:Sprite):void 
		{
			// Escuchar los eventos relevantes.
			preloader.addEventListener( ProgressEvent.PROGRESS, handleProgress );
			preloader.addEventListener( Event.COMPLETE, handleComplete );
			preloader.addEventListener( FlexEvent.INIT_PROGRESS, handleInitProgress );
			preloader.addEventListener( FlexEvent.INIT_COMPLETE, handleInitComplete );
		}
		
		// Inicializar el control Loader en el método initialize del interface IPreloaderDisplay
		override public function initialize():void 
		{	
//			this.width = this.stage.width;
//			this.height = this.stage.height;
			dpbImageControl = new Loader();
			dpbImageControl.contentLoaderInfo.addEventListener( Event.COMPLETE, loader_completeHandler );
			dpbImageControl.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, errorHandler );
//			dpbImageControl.load(new URLRequest("assets/loading/loading.swf"));
			dpbImageControl.load(new URLRequest("assets/background/beijing.jpg"));
		}
		
		private function errorHandler( event:Event ):void
		{
			
		}
		// Después de cargar el SWF
		private var bitmap:BitmapData;
		private function loader_completeHandler(event:Event):void 
		{	
			bitmap = (dpbImageControl.content as Bitmap).bitmapData;
			dpbImageControl.contentLoaderInfo.removeEventListener(Event.COMPLETE , loader_completeHandler );
			
			var g:Graphics = this.graphics;
			g.clear();
			g.beginBitmapFill( bitmap , null ,true );
			g.drawRect( 0 , 0 , this.stage.stageWidth , this.stage.stageHeight );
			g.endFill();
			
			
			var bg0:DisplayObject = new bg0();
			this.addChild( bg0 );
			bg0.height = 30;
			bg0.width = this.stage.width;
			
			var bg1:DisplayObject = new bg1();
			this.addChild( bg1 );
			bg1.height = this.stage.stageHeight;
			bg1.x = (this.stage.stageWidth - 900)/2 - bg1.width;
			var bg2:DisplayObject = new bg2();
			this.addChild( bg2 );
			bg2.height = this.stage.stageHeight;
			bg2.x = (this.stage.stageWidth - 900)/2 + 900;
			var bg3:DisplayObject = new bg3();
			this.addChild( bg3 );
			bg3.height = 180;
			bg3.width = this.stage.stageWidth;
			bg3.y = this.stage.stageHeight - 180;
			
			var v1:DisplayObject = new v1();
			this.addChild( v1 );
			v1.x = this.stage.stageWidth/2 - v1.width/2;
			v1.y = this.stage.stageHeight/2 - v1.height/2 - 50;
			
			var loader:MovieClip = new loading();
			this.addChild( loader );
			loader.x = this.stage.stageWidth/2 - loader.width/2 + 30;
			loader.y = this.stage.stageHeight/2 - loader.height/2 + 50;
		}
		
		// Definir los oyentes vacíos.
		private function handleProgress(event:ProgressEvent):void 
		{
			/*var relacion:Number = event.bytesLoaded / event.bytesTotal;
			
			if (dpbImageControl.content)
			{
				dpbImageControl.content["myMask"].scaleX = relacion;
				dpbImageControl.content["txt"].text = Math.round(relacion * 100 ) + "%";
			}*/
		}
		
		private function handleComplete(event:Event):void 
		{
		}
		private function handleInitProgress(event:Event):void 
		{
		}
		
		private function handleInitComplete(event:Event):void 
		{
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}